var tab;

layui.use(['element', 'layer'], function () {
    var element = layui.element(),
        $ = layui.jquery,
        layer = layui.layer;
        
        
    //iframe自适应
    $(window).on('resize', function () {
        var winHeight = $(this).height();
    	var headerHeight = $("#admin-header").height();
    	var footerHeigt = $("#admin-footer").height();
    	var admin_body = $("#admin-body");
    	var $content = $("#admin-body .layui-tab-content");
    	var tabtitleHeight = $("#layui-tab-title").outerHeight();
    	admin_body.height(winHeight - headerHeight - footerHeigt);
        $content.height(admin_body.height() - tabtitleHeight - 20);
    }).resize();

    //监听点击事件

	//头部导航上下切换
    var navbox = $("#navtop-box");
    var headHeight = +$("#admin-header").height();
    var navboxHeight = +navbox.height();
    var navboxTop = 0;
    if(headHeight == navboxHeight){
        $("#nav-updown").hide();
    }else{
        $("#nav-updown").show();
    }
    $("#nav-updown>span").on("click", function(){
        
    	if($(this).data("updown") == "up"){//上移
            console.log(navboxTop);
            if(navboxTop > 0){
                navboxTop -= headHeight;
                navbox.css("margin-top", (-navboxTop) + 'px');
                $(this).siblings().addClass("active");
                if(navboxTop == 0){
                    $(this).removeClass("active");
                }
            }
    	}else{//下移
            if(navboxTop < (navboxHeight - headHeight)){
                navboxTop += headHeight;
                navbox.css("margin-top", (-navboxTop) + 'px');
                $(this).siblings().addClass("active");
                if(navboxTop == (navboxHeight - headHeight)){
                    $(this).removeClass("active");
                }
            }
    	}
    })
    //左导航隐藏，显示
    $('#admin-side').on('click', '#admin-side-toggle', function () {
        var sideWidth = $('#admin-side').width();
        var _this = $(this);
        var leftsideW = 200;
        if (sideWidth > 0) {
        	_this.css({
        		"background-position": "-19px 0",
        		"right": "-19px"
        	});
            $('#admin-footer').animate({
                "left": '0'
            });
            $("#admin-body").animate({
            	"marginLeft": "0"
            });
            $('#admin-side, #admin-side .scrollbox, #admin-side .layui-nav-item-title').animate({
                "width": '0'
            });
            $('.build-leftmenu').css({
            	"overflow": "hidden"
            });
        } else {
        	_this.css({
        		"background-position": "0 0",
        		"right": "0"
        	});
            $('#admin-footer').animate({
                left: leftsideW + "px"
            });
            $("#admin-body").animate({
            	"marginLeft": leftsideW + "px"
            })
            $('#admin-side').animate({
                "width": leftsideW - 1 +'px'
            });
            $('#admin-side .scrollbox, #admin-side .layui-nav-item-title').animate({
                "width": '100%'
            });
            $('.build-leftmenu').css({
            	"overflow": "initial"
            });
        }
    });
        
//  //左边导航只显示一个
//  $("#admin-side .layui-nav-item").on("click", function(){
//  	$(this).siblings().removeClass('layui-nav-itemed');
//  })
    
    //左边导航,生成tab
    //触发事件
	  var active = {
	    tabAdd: function(othis){
	      //新增一个Tab项
	      	element.tabAdd('admin-tab', {
		        title: othis.title, 
		        content: '<iframe src=' + othis.url + ' width="100%" height="100%" frameborder="no"></iframe>',
		        id: othis.id
		    })
	    },
	    tabDelete: function(othis){
	      //删除指定Tab项
	      element.tabDelete('admin-tab', othis.id); //删除
	      
	      
	      othis.addClass('layui-btn-disabled');
	    },
	    tabChange: function(othis){
	      //切换到指定Tab项
	      element.tabChange('admin-tab', othis.id); //切换
	    }
	  };
      window.active = active;
	  
	  $('#admin-side').on('click', '.addtab-active', function(){
	    var othis = $(this), 
	    	type = othis.data('type');
        var dataArr = {
            url: $(this).data('url'),
            title: $(this).data('title'),
            id: $(this).data('id')
        }
	    var canadd = true;
	    $("#layui-tab-title li").each(function(){//判断tab是否已存在
	      	if($(this).attr('lay-id') ==  othis.data('id')){
	      		canadd = false;
	      	}
	    })
	    if(canadd){
	      	active[type] ? active[type].call(this, dataArr) : '';
	    }
	    active['tabChange'].call(this, dataArr);
	  });
});

function parent_tab(arr){
    var hasdo = true;
    $("#layui-tab-title li").each(function(index, el) {
        var thisTitle = $(this).find('span').length > 0 ? $(this).find('span').text() : $(this).text().replace('ဆ','');
        var thisUrl = $('#layui-tab-content').find('.layui-tab-item').eq(index).find('iframe').length>0 ? 
            $('#layui-tab-content').find('.layui-tab-item').eq(index).find('iframe').attr('src') : '';
        if(thisTitle == arr.title && thisUrl != arr.url){
            hasdo = false;
            layui.use(['layer'], function(){
                layer = layui.layer;
                layer.alert('此标题已存在，请另起标题!');
                return false;
            })
        }else if(thisTitle == arr.title && thisUrl == arr.url){
            hasdo = false;
            var thisId = $(this).attr('lay-id');
            active.tabChange({
                id: thisId
             });
            return false;
        }
    });
    if(hasdo){
        active.tabAdd({
        url: arr.url,
        title: arr.title,
        id: arr.id
     });
     active.tabChange({
        id: arr.id
     });
    }
}
